package com.cashmama.market.api.domain.service;

import com.cashmama.market.api.domain.vo.message.MessageListInfoVO;
import com.cashmama.market.api.domain.vo.message.MessageListVO;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.msg.push.PushMsgService;
import com.fqgj.msg.ro.PushMsgConditionRequestRO;
import com.fqgj.msg.ro.PushMsgInfoRO;
import com.fqgj.msg.utils.Paged;
import com.fqgj.msg.utils.Result;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-30 10:57
 **/
@Service
public class MessageService {

    @Autowired
    private PushMsgService pushMsgService;

    /**
     *
     * @param userCode
     * @param pageNo
     * @param msgType
     * @return
     */
    public MessageListInfoVO getUserMessageList(String userCode, Integer pageNo, String msgType){

        MessageListInfoVO messageListInfoVO = new MessageListInfoVO();
        Boolean hasUnreadNotice = false;
        Page page = new Page();
        page.setCurrentPage(pageNo);
        PushMsgConditionRequestRO pushMsgConditionRequestRO = new PushMsgConditionRequestRO();
        pushMsgConditionRequestRO.setUserCode(userCode);
        pushMsgConditionRequestRO.setCurrentPage(page.getCurrentPage());
        pushMsgConditionRequestRO.setPageSize(page.getPageSize());
        pushMsgConditionRequestRO.setMsgType(msgType);
        Result<Paged<List<PushMsgInfoRO>>> result = pushMsgService.queryPushMsgByCondition(pushMsgConditionRequestRO);
        if(!result.isSuccess()){
            return messageListInfoVO;
        }
        Paged<List<PushMsgInfoRO>> messagePushList = result.getData();
        List<MessageListVO> messageList = new ArrayList<>();
        if (null == messagePushList || CollectionUtils.isEmpty(messagePushList.getData())) {
            return messageListInfoVO;
        }
        List<PushMsgInfoRO> data = messagePushList.getData();
        List<Long> unreadMsgIds = Lists.newArrayList();
        for (PushMsgInfoRO message : data) {
            MessageListVO vo = new MessageListVO();
            vo.setMessageID(message.getMsgId());
            String sendTime = DateUtil.getDate(message.getCreateTime(), "yyyy-MM-dd HH:mm:ss");
            vo.setGmtCreate(sendTime);
            vo.setContent(message.getContent());
            vo.setTitle(message.getTitle());
            messageList.add(vo);
            if(message.getStatus() == 0){
                unreadMsgIds.add(message.getMsgId());
            }
        }
        //打开未读消息列表自动读
        if(CollectionUtils.isNotEmpty(unreadMsgIds)){
            pushMsgService.readPushMsgs(unreadMsgIds);
        }
        if ("notice".equals(msgType)) {
            Result<Integer> countNotice = pushMsgService.getUnReadPushNoticeNum(userCode);
            hasUnreadNotice = countNotice.isSuccess() && countNotice.getData() != null && countNotice.getData() > 0;
        }
        messageListInfoVO.setHasUnreadNotice(hasUnreadNotice);
        messageListInfoVO.setMessages(messageList);
        messageListInfoVO.setHasNextPage(messagePushList.getTotalPage() > messagePushList.getCurrentPage());
        return messageListInfoVO;
    }
}
